/* Copyright (C) 2013 Guangzhou Tronlong Electronic Technology Co., Ltd. - www.tronlong.com */

#ifndef _UTIL_H_
#define _UTIL_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>

#define ERRSTR strerror(errno)

/**
 * @brief A macro for robust error handling.
 *
 * If the condition `cond` is true, it prints an error message including
 * the file and line number, and then returns -1 from the current function.
 * This allows for graceful cleanup in calling functions.
 * It replaces the old BYTE_ON macro that would abort the program.
 */
#define RETURN_ON_FAIL(cond, ...) \
    do { \
        if (cond) { \
            fprintf(stderr, "ERROR(%s:%d) : ", __FILE__, __LINE__); \
            fprintf(stderr,  __VA_ARGS__); \
            return -1; \
        } \
    } while(0)

#endif
